<?php

declare (strict_types=1);

namespace App\Model;

/**
 * @property int $id
 * @property int $uid
 * @property string $order_no
 * @property int $aid
 * @property int $goods_id
 * @property int $num
 * @property string $currency
 * @property string $price
 * @property string $price_unit
 * @property string $discount_amount
 * @property string $coupon_amount
 * @property string $pay_price
 * @property string $buy_price
 * @property string $pay_price_unit
 * @property string $buy_price_unit
 * @property string $service_amount
 * @property string $tax_amount
 * @property string $refundable_amount
 * @property string $refund_amount
 * @property int $pay_status
 * @property int $status
 * @property int $create_time
 * @property int $update_time
 * @property int $pay_time
 * @property int $cancel_time
 * @property int $expire_time
 * @property string $remark
 * @property string $process
 */
class Order extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'order';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'uid' => 'integer', 'aid' => 'integer', 'goods_id' => 'integer', 'num' => 'integer', 'pay_status' => 'integer', 'status' => 'integer', 'create_time' => 'integer', 'update_time' => 'integer', 'pay_time' => 'integer', 'cancell_time' => 'integer', 'expire_time' => 'integer'];

    public static function getOrderNo()
    {

        $orderNo = 's' . date('ymd', time()) . substr(microtime(), 2, 3) . sprintf('%05d', rand(0, 99999));
        $exists = self::where(['order_no' => $orderNo])->exists();
        if ($exists) {
            self::getOrderNo();
        } else {
            return $orderNo;
        }

    }

    public static function getOrderList($where)
    {
        $select = [
            'a.id',
            'a.order_no',
            'a.uid',
            'a.aid',
            'a.goods_id',
            'a.num as goodsNum',
            'a.status',
            'a.currency',
            'a.currency_prefix',
            'a.use_now',
            'a.chat_type',
            'a.pay_price',
            'a.pay_price_unit',
            'a.expire_time',
            'a.create_time',
            'd.username as num',
            'b.name as nickname',
            'b.type',
            'b.direction',
            'b.language',
            'b.cover',
            'd.online',
            'd.service_status',
            'c.self_name as countryName',
            'c.country_img',
            'e.duration'
        ];


        $model = ConsultantDatum::from('order as a')
            ->leftJoin('consultant_datum as b', 'a.aid', '=', 'b.uid')
            ->leftJoin('country as c', 'b.country', '=', 'c.id')//国家表
            ->leftJoin('chat_user as d', 'b.uid', '=', 'd.uid')
            ->leftJoin('consultant_goods as e', 'a.goods_id', '=', 'e.id')
            ->where($where)
            ->select($select)
            ->orderBy('a.id', 'desc');


        $list = page($model);


        return $list;

    }


}
